<?php

namespace App\Util;

use Monolog\Processor\ProcessorInterface;

/**
 * @method void emergency(string $message, array $context = [])
 * @method void alert(string $message, array $context = [])
 * @method void critical(string $message, array $context = [])
 * @method void error(string $message, array $context = [])
 * @method void warning(string $message, array $context = [])
 * @method void notice(string $message, array $context = [])
 * @method void info(string $message, array $context = [])
 * @method void debug(string $message, array $context = [])
 * @method void log($level, string $message, array $context = [])
 *
 * @see \Illuminate\Log\Logger
 */
class LogUtil
{
    /* @var $logger \Illuminate\Log\Logger */
    protected $logger;
    /**
     * @var string 标题
     */
    protected $title;
    protected $traceId;
    protected $jobParams;
    protected $cmdParams;

    /**
     * @var static 单例模式
     */
    protected static $instance;

    /**
     * 单例模式
     */
    public static function getInstance()
    {
        if (static::$instance instanceof static) {
            return static::$instance;
        }
        static::$instance = new static();
        return static::$instance;
    }

    public function getTitle()
    {
        return $this->title;
    }

    public function setTitle(string $title)
    {
        $this->title = $title;
    }

    /**
     * @return mixed
     */
    public function getTraceId()
    {
        return $this->traceId;
    }

    /**
     * @param mixed $input
     */
    public function setTraceId($input = ''): void
    {
        if ($input) {
            $this->traceId = $input;
        }
        else {
            $this->traceId = $this->generateTraceId(32);
        }
    }

    private function generateTraceId($length)
    {
        return substr(hash('md5', uniqid('', true)), 0, $length);
    }

    public function setJobParams($params)
    {
        $this->jobParams = $params;
    }

    public function getJobParams()
    {
        return $this->jobParams;
    }

    /**
     * @return mixed
     */
    public function getCmdParams()
    {
        return $this->cmdParams;
    }

    /**
     * @param mixed $cmdParams
     */
    public function setCmdParams($cmdParams): void
    {
        $this->cmdParams = $cmdParams;
    }
}
